<?php
declare (strict_types = 1);

namespace app\admin\controller\setting;

use app\admin\controller\AuthController;
use app\admin\model\system\SystemMenus as MenusModel;
use bbweb\services\UtilService as Util;
use think\facade\Route as Url;
use think\Request;

class SystemMenus extends AuthController
{
    /*
     * 显示资源列表
     */
    public function index(Request $req)
    {
        if ($req->isPost()) {
            $array = MenusModel::menuAll();
            $this->success('ok','', $array, 0);
        }
        $sonAuth = MenusModel::getSonAuth($req->controller(),$req->action(),array_keys($this->auth));
        $this->assign('sonAuth',$sonAuth);
        return $this->fetch();
    }

    /**
     * 添加后台菜单
     */
    public function add(Request $req)
    {
        // 执行
        if ($req->isPost()) {
            $data = Util::postMore([
                'menu_name',
                'controller',
                'action',
                'icon',
                ['pid', 0],
                ['sort', 0],
                ['is_show', 1],
                ['type', 1],
                ['visable', 0]
            ], $req);
            if (!$data['menu_name']) $this->error('请输入按钮名称');
            $data['url'] = Url::buildUrl( '/' . $data['controller'] . '/' . $data['action'])->build();
            $res = MenusModel::create($data);
            if($res) $this->success('添加菜单成功!');
            else $this->error('添加菜单失败');
        }
        $id = $req->param('id/d');
        if($id) {
            $menu = MenusModel::get($id);
        } else {
            $menu = ['controller'=>''];
        }
        $this->assign('id',$id);
        $this->assign('menu', $menu);
        return $this->fetch();
    }

    /**
     * 修改后台菜单
     */
    public function edit(Request $req)
    {
        // 获取编号
        $id = $req->param('id/d');
        if (!$id) $this->error('参数错误！');

        $menu = MenusModel::get($id);
        if (empty($menu)) $this->error('数据不存在!');
        // 执行登录
        if ($req->isPost()) {
            $data = [];
            if($req->param('change/d')) {
                $data['visable'] = $req->param('visable/d');
            } elseif($req->param('op/d')==1) {
                $data['sort'] = $req->param('sort/d');
            } else {
                $data = Util::postMore([
                    'menu_name',
                    'controller',
                    'action',
                    'icon',
                    ['pid', 0],
                    ['is_show', 1],
                    ['type', 1],
                    ['visable', 1]
                ], $req);
                if (!$data['menu_name']) $this->error('请输入按钮名称');
                $data['url'] = Url::buildUrl( '/' . $data['controller'] . '/' . $data['action'])->build();
            }
            $res = MenusModel::edit($data,$id);
            if($res) $this->success('修改菜单成功!');
            else $this->error('修改菜单失败');
        }

        $this->assign('menu',$menu);
        return $this->fetch();
    }

}
